/** \file   common.c
 *
 *  \brief  This file contains functions, prototypes, structures, macros, constants
 *  shared by multiple applications
 *
 *
 *   Note: N/A
 *
 *  \author     Jiawei Xie
 *
 *  \version    0.1   Created newly for NAVCON
 */


#include <std.h>
#include <stdio.h>
#include <log.h>
#include <clk.h>
#include <tsk.h>
#include <gbl.h>
#include <gio.h>
#include "common.h"

/* ========================================================================== */
/*                          BIOS related Function                             */
/* ========================================================================== */


/**
 * \brief  GIO_setup() use as general IOM handler setup fxn.
 *  *
 * \param    devname - define the device name e.g. /UARTx
 * 			 dir	 - define the direction of the handler IOM_OUTPUT/IOM_INPUT
 *			 nPacket - number of async packets
 *			 EN_EDMA - if true, enable EDMA mode
 * \return   NULL if error, *status contains BIOS return code
 */
GIO_Handle GIO_setup(Int32 *status, String devname, int dir, int nPacket, Ptr chanParams)
{
    GIO_Attrs gioAttrs = GIO_ATTRS;
    GIO_Handle handler = NULL;

    /* Input Parameters Checks */
    if(devname == NULL){
    	return NULL;
    }
    // Recognize handler that need to be updated
    if(dir != IOM_OUTPUT && dir != IOM_INPUT){
    	return NULL;
    }
    if(nPacket <= 0){
    	return NULL;
    }

    /* Initialize channel attributes.*/
    gioAttrs.nPackets = nPacket;

    //Perform syscall to allocate GIO handler
    handler = GIO_create(devname,dir,status, chanParams,&gioAttrs);

    return handler;
}



/* ========================================================================== */
/*                          General Function                                  */
/* ========================================================================== */




void bzeros(unsigned char * buf, int size){
	int i = 0;

	for(i=0;i<size;i++){
		buf[i]=0;
	}

}

void print_buf(unsigned char * buf, int size){
	int i = 0;
	printf("data[] = ",buf[i]);
	for(i=0;i<size;i++){
		printf("%d ",buf[i]);
	}
	printf("\n",buf[i]);
}

unsigned int ms_conv_to_tick(int timeout){

    unsigned long freq;
    float milliSecsPerIntr, cycles;
    unsigned int timeout_tick = 0;
	/* get frequency of platform */
	freq = GBL_getFrequency();
	/* get cpu cycles per ltime interrupt */
	cycles = CLK_cpuCyclesPerLtime();
	/* computer milliseconds per ltime interrupt */
	milliSecsPerIntr = cycles/(float)freq;
	/* compute timeout */
	timeout_tick = timeout/milliSecsPerIntr;

	return timeout_tick;
}
